<?php

namespace api\web\controller;

use api\web\model\NewModel;
use cmf\controller\RestBaseController;

class NewController extends RestBaseController
{
    public function index(){
        //搜索
        $keyword = input('keyword','2');
        $where=[];
        if ($keyword){
            $where[]=['title|author','like',"%$keyword"];
        }
        //页码
        $page =input('page/d',1);
        //每页显示的条数
        $pagesize = input('pagesize/d',5);
        $new=NewModel::order('list_order','DESC')
            ->field('id,title,image,create_time,content')
            ->where($where)
            ->page($page,$pagesize)
            ->select();
        $this->success('新闻列表',$new);
    }
    public function getnews(){
        $id= input('id/d');
        if(empty($id)) {
            $this->error('请输入一个id');
        }
        $News = NewModel::withoutField('delete_time,list_order')
            ->find($id);
        if(!$News){
            $this->error('新闻不存在');
        }
        $content1=htmlspecialchars_decode($News['content']);
        $content2=cmf_replace_content_file_url($content1,true);
        $data['content']=$content2;
        $this->success('获取详细新闻列表',$News);
    }
}